package com.example.fp.zxd.controller

import com.example.fp.cgc.code.logAnno.LogAnno
import com.example.fp.cgc.entity.PageCondition
import com.example.fp.zxd.entity.Gzh
import com.example.fp.zxd.mapper.GzhMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.servlet.ModelAndView
import kotlin.math.min


/**
 *
 */
@Controller
@RequestMapping
open class GzhWebController {

    @Autowired
    @Lazy
    lateinit var gzhMapper: GzhMapper


    @LogAnno(operateType = "查询当前应用列表")
    @RequestMapping(value = ["/gzhlist/{page}"])
    open fun getGzhListPage(@PathVariable("page") page: Int, model: Model): ModelAndView? {

        val count: Int = gzhMapper.selectgzhsum()
        if (count == 0) {
            model.addAttribute("count", count)
            return ModelAndView("gzhlist/gzhlist", "gzhModel", model)
        } else {
            var page = page
            if (page == 0)
                page = 1
            val pagesize = 10


            val maxpage = if (count % pagesize == 0) count / pagesize else count / pagesize + 1
            if (page > maxpage) {
                page = maxpage
            }
            val temp = (page - 1) * pagesize
            val pc = PageCondition(temp, pagesize, "")
            val gzhList: List<Gzh> = gzhMapper.selectGzhListPage(pc)

            val currMaxPage = min(page + 10, maxpage)

            model.addAttribute("gzhList", gzhList)
            model.addAttribute("pageNo", page)
            model.addAttribute("count", count)
            model.addAttribute("maxpage", maxpage)
            model.addAttribute("currMaxPage", currMaxPage)

            return ModelAndView("gzhlist/gzhlist", "gzhModel", model)
        }
    }

    @RequestMapping(value = ["/hsuccess"])
    open fun success(model: Model): ModelAndView {
        return ModelAndView("gzhlist/hsuccess", "gzhModel", model)
    }

    /**
     *
     */
    @GetMapping("/gzhForm")
    open fun createForm(model: Model): ModelAndView? {
        model.addAttribute("gzh", Gzh())
        model.addAttribute("title", "创建用户")
        return ModelAndView("gzhlist/gzhadd", "gzhModel", model)
    }

    /**
     *
     */
    @PostMapping("/addOrModityGzh")
    open fun saveOrUpdateUser(gzh: Gzh): ModelAndView {
        val gzh1 = gzhMapper.selectById(gzh.id)
        if (gzh1 == null) {
            gzhMapper.insert(gzh)
            return ModelAndView("redirect:/hsuccess") //重定向到list页面
        } else {
            val count = gzhMapper.getGzhIndex(gzh.id)
            val i = count % 10
            var page = count / 10
            if (i > 0) {
                page += 1
            }
            gzhMapper.updateById(gzh)
            return ModelAndView("redirect:/gzhlist/${page}") //重定向到list页面
        }

    }

    /**
     *
     */
    @GetMapping("/deleteGzh/{id}&{page}")
    open fun deleteUser(@PathVariable("id") id: Int, @PathVariable("page") page: Int): ModelAndView? {
        gzhMapper.deleteById(id)
        return ModelAndView("redirect:/gzhlist/$page") //重定向到list页面
    }

    @GetMapping("/delSelectedGzh/{checkedID}&{page}")
    open fun delSelectedGzh(@PathVariable("checkedID") checkedID: String, @PathVariable("page") page: Int): ModelAndView {
        val ids = (checkedID as java.lang.String).split(",")
        for (i in ids.indices) {
            gzhMapper.deleteById(ids[i].toInt())
        }
        return ModelAndView("redirect:/gzhlist/$page") //重定向到list页面
    }

    /**
     *
     */
    @GetMapping("/modifyGzh/{id}")
    open fun modifyUser(@PathVariable("id") id: Int, model: Model): ModelAndView? {
        model.addAttribute("gzh", gzhMapper.selectById(id))
        model.addAttribute("title", "修改应用")
        return ModelAndView("gzhlist/modifyGzh", "gzhModel", model)
    }
}